{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 一、本地运行调试代码"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "网络结构在main.py之后，执行`run main.py`可以本地调试。\n",
    "\n",
    "执行下面代码可使用快捷键`shift+enter`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "run main.py"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 二、提交到GPU训练"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Windows环境执行"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过执行训练命令，`-e=`是整个数据集循环10次，`-b=`是每次训练读取的数据量为32 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "! flyai.exe train -e=10 -b=32"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Mac和Linux执行"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "给执行脚本授权"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "! chmod +x ./flyai"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过执行训练命令，整个数据集循环10次，每次训练读取的数据量为 32 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "! ./flyai train -e=10 -b=32"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 三、FlyAI的Python环境"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Windows用户，Python路径在："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "! %USERPROFILE%\\.flyai\\env\\python.exe"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### pip路径："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "! %USERPROFILE%\\.flyai\\env\\Scripts\\pip.exe list"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 如本地遇到No module name 'xxx'问题时可以执行下面代码安装"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "! %USERPROFILE%\\.flyai\\env\\Scripts\\pip.exe install xxx -i https://pypi.flyai.com/simple "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Mac和Linux用户，Python路径在："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "! ~/.flyai/env/bin/python3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### pip路径："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "! ~/.flyai/env/bin/pip list"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 如本地遇到No module name 'xxx'问题时可以执行下面代码安装"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "! ~/.flyai/env/bin/pip install xxx -i https://pypi.flyai.com/simple"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 四、样例代码说明\n",
    "\n",
    "##### `app.yaml`\n",
    "\n",
    "> 是项目的配置文件，项目目录下**必须**存在这个文件，是项目运行的依赖。\n",
    "\n",
    "##### `processor.py`\n",
    "\n",
    "> **样例代码中已做简单实现，可供查考。**\n",
    ">\n",
    "> 处理数据的输入输出文件，把通过csv文件返回的数据，处理成能让程序识别、训练的矩阵。\n",
    ">\n",
    "> 可以自己定义输入输出的方法名，在`app.yaml`中声明即可。\n",
    ">\n",
    "> ```python\n",
    ">     def input_x(self, image_path):\n",
    ">         '''\n",
    ">     \t参数为csv中作为输入x的一条数据，该方法会被dataset.next_train_batch()\n",
    ">     \t和dataset.next_validation_batch()多次调用。可在该方法中做数据增强\n",
    ">     \t该方法字段与app.yaml中的input:->columns:对应\n",
    ">     \t'''\n",
    ">         pass\n",
    ">\n",
    ">     def output_x(self, image_path):\n",
    ">          '''\n",
    ">     \t参数为csv中作为输入x的一条数据，该方法会被dataset.next_train_batch()\n",
    ">     \t和dataset.next_validation_batch()多次调用。\n",
    ">     \t该方法字段与app.yaml中的input:->columns:对应\n",
    ">     \t'''\n",
    ">         pass\n",
    ">\n",
    ">     def input_y(self, labels):\n",
    ">         '''\n",
    ">         参数为csv中作为输入y的一条数据，该方法会被dataset.next_train_batch()\n",
    ">     \t和dataset.next_validation_batch()多次调用。\n",
    ">     \t该方法字段与app.yaml中的output:->columns:对应\n",
    ">         '''\n",
    ">         pass\n",
    ">\n",
    ">     def output_y(self, data):\n",
    ">         '''\n",
    ">         输出的结果，会被dataset.to_categorys(data)调用\n",
    ">         :param data: 预测返回的数据\n",
    ">         :return: 返回预测的标签\n",
    ">         '''\n",
    ">         pass\n",
    ">\n",
    "> ```\n",
    "\n",
    "##### `main.py`\n",
    "\n",
    "> **样例代码中已做简单实现，可供查考。**\n",
    ">\n",
    "> 程序入口，编写算法，训练模型的文件。在该文件中实现自己的算法。\n",
    ">\n",
    "> 通过`dataset.py`中的`next_batch`方法获取训练和测试数据。\n",
    ">\n",
    "> ```python\n",
    "> '''\n",
    "> Flyai库中的提供的数据处理方法\n",
    "> 传入整个数据训练多少轮，每批次批大小\n",
    "> '''\n",
    "> dataset = Dataset(epochs=args.EPOCHS, batch=args.BATCH)\n",
    "> #获取训练数据\n",
    "> x_train, y_train = dataset.next_train_batch()\n",
    "> #获取验证数据\n",
    "> x_val, y_val = dataset.next_validation_batch()\n",
    "> ```\n",
    ">\n",
    "> 通过`model.py`中的`save_model`方法保存模型\n",
    ">\n",
    "> ```python\n",
    "> # 模型操作辅助类\n",
    "> model = Model(dataset)\n",
    "> model.save_model(YOU_NET)\n",
    "> ```\n",
    ">\n",
    "> **如果使用`PyTorch`框架，需要在`net.py`文件中实现网络。其它用法同上。**\n",
    "\n",
    "##### `model.py`\n",
    "\n",
    "> **样例代码中已做简单实现，可供查考。**\n",
    ">\n",
    "> 训练好模型之后可以继承`flyai.model.base`包中的`base`重写下面三个方法实现模型的保存、验证和使用。\n",
    ">\n",
    "> ```python\n",
    ">    def predict(self, **data):\n",
    ">         '''\n",
    ">         使用模型\n",
    ">       \t:param data: 模型的输入的一个或多个参数\n",
    ">         :return:\n",
    ">         '''\n",
    ">         pass\n",
    ">\n",
    ">     def predict_all(self, datas):\n",
    ">         '''\n",
    ">         （必须实现的方法）评估模型，对训练的好的模型进行打分\n",
    ">       \t:param datas: 验证集上的随机数据，类型为list\n",
    ">         :return outputs: 返回调用模型评估之后的list数据\n",
    ">         '''\n",
    ">         pass\n",
    ">\n",
    ">     def save_model(self, network, path=MODEL_PATH, name=MODEL_NAME, overwrite=False):\n",
    ">         '''\n",
    ">         保存模型\n",
    ">         :param network: 训练模型的网络\n",
    ">         :param path: 要保存模型的路径\n",
    ">         :param name: 要保存模型的名字\n",
    ">         :param overwrite: 是否覆盖当前模型\n",
    ">         :return:\n",
    ">         '''\n",
    ">         self.check(path, overwrite)\n",
    ">\n",
    "> ```\n",
    "\n",
    "##### `predict.py`\n",
    "\n",
    ">**样例代码中已做简单实现，可供查考。**\n",
    ">\n",
    ">对训练完成的模型使用和预测。\n",
    "\n",
    "##### `path.py`\n",
    "\n",
    "> 可以设置数据文件、模型文件的存放路径。\n",
    "\n",
    "##### `dataset.py`\n",
    "\n",
    "> 该文件在**FlyAI开源库**的`flyai.dataset`包中，通过`next_train_batch()`和`next_validation_batch()`方法获得`x_train` `y_train` `x_val` `y_val`数据。\n",
    ">\n",
    "> FlyAI开源库可以通过`pip3 install -i https://pypi.flyai.com/simple flyai` 安装。\n",
    "\n",
    "***\n",
    "\n",
    "### [FlyAI全球人工智能专业开发平台，一站式服务平台](https://flyai.com)\n",
    "\n",
    "**扫描下方二维码，及时获取FlyAI最新消息，抢先体验最新功能。**\n",
    "\n",
    "\n",
    "\n",
    "[![GPL LICENSE](https://www.flyai.com/images/coding.png)](https://flyai.com)\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
